import {
  hotSearch
} from '@/static/js/problem.js';
import {
  getProblemSearch,
  readJsonFileContent
} from '@/static/js/search.js';
import {
  hightLightText, parseTimeDot
} from "@/utils/utils.js"
export default {
  data() {
    return {
      hightLightText,//高亮函数
      parseTimeDot,//时间格式化
      hotPlaceholderStr: '', //热门搜索
      isInputFocus: false, //是否聚焦
      keyword: '', //输入框的值
      historyList: [], //历史记录
      hotSearchList: [], //热门搜索
      isShowSearchList: false, //是否显示搜索结果
      searchList: [], //搜索结果
      isLoading: false, //是否正在加载
      page: 1,
      pageSize: 15,
      total: 0, //总条数
      status: 'more', //加载更多状态
      isRefreshing: false, //是否正在刷新
      searchList: [], //搜索结果JSON数据
      isSearch: false, //是否搜索过
      dataList: [], //搜索结果处理后数据
      tagStyle: {
        em: 'font-style: normal;color: #fa3534;'
      },//样式
    };
  },
  onLoad(e) {
    this.getHotSearch();
    //获取历史搜索记录
    this.historyList = uni.getStorageSync('wtHistoryList') || [];
  },
  onShow() { },
  methods: {
    //请求热门搜索
    getHotSearch() {
      hotSearch({
        type: 1
      }).then(res => {
        if (res.code == 1) {
          this.hotSearchList = res.data;
          this.hotPlaceholderStr = res.data[0].content
        }
      });
    },

    //删除搜索历史记录
    deleteHistorySearch() {
      uni.removeStorageSync('wtHistoryList');
      this.historyList = [];
    },

    //点击搜索
    clickSearch() {
			this.page = 1;
			this.pageDataList = [];
			this.dataList = []
      this.total = 0
			this.search(this.keyword)
		},

    //搜索
    search(value) {
      this.keyword = value;
      this.isLoading = true;
      let historyList = this.historyList;
      let params = {
        query: value,
        retrieveFields: ['title'],
        page: this.page,
        pageSize: this.pageSize,
      };
      if (value == '' || value == undefined || value == null) {
        this.$tools.msg('请输入搜索内容')
        return
      } else {
        if (historyList.indexOf(value) == -1) {
          historyList.unshift(value);
          if (historyList.length == 11) {
            historyList.pop(value);
          }
        }
        uni.setStorageSync('wtHistoryList', historyList);
      }
      // 请求数据
      getProblemSearch(params).then(res => {
        if (res.status == 0) {
          this.total = res.data.resultTotal;
          this.queryKeywords = res.data.queryKeywords;
          if (res.data.result.length >= this.pageSize) {
            this.status = 'more';
          } else {
            this.status = 'noMore';
          }
          this.searchList = res.data.result;
          this.isRefreshing = false;
          this.getPageData();
        }
      });
    },


    //json数据处理
    getPageData() {
      for (let i = 0; i < this.searchList.length; i++) {
        let item = this.searchList[i];
        readJsonFileContent(item.matchDataUrl).then(res => {
          this.dataList.push(res);
        });
      }
      setTimeout(() => {
        let arr = JSON.parse(JSON.stringify(this.dataList))
        this.dataList = arr
        this.isSearch = true;
        uni.hideLoading();
        this.isLoading = false;
      }, 300);
    },


    //下拉刷新
    reload() {
      this.page = 1;
      this.searchList = [];
      this.dataList = []
      this.isRefreshing = true;
      this.search(this.keyword)
    },

    //加载更多
    loadMore() {
      if (this.status == 'more') {
        this.page++;
        this.search(this.keyword)
      } else {
        this.$tools.msg('没有更多了');
      }
    },

    //跳转详情
    toInfo(e) {
      uni.navigateTo({ url: `/library/problem/details/index?id=${e.id}` })
    },
  },
}